Funnotes Logo
Home Sai Satcharitra Talapatram

An Intuitive & Easy-To-Use OpenGL Colour Class

[BACK]

Any one who deals with OpenGL programming will always encounter the need to render his objects in multiple colours. For that he will always have to go and look out for the RGB vlaues of the colours that he want the objects to be rendered in. He knows the colour. But he only knows it by name, not by its RGB components. Also it is almost impossible to remember RGB components of each and every colour that we encounter.

So, I wrote this class for myself. I wrote it while I was doing my MSc project. Eventhough I did not needed all the colours, I hand coded mostly and seldom used colours so that it will never again become a necessity for me to find out RGB values for certain colour. I coded 109 colours. I coded even seldom used colours like Khaki and Indian Red.

So, it is a C++ class. I coded all the colours into a C++ class. It uses the Singleton design pattern. Once initialised you can use this class anywhere in your application. Even there is a guard against multiple-initializations. As you will be dealing with a Singleton object when using this class, it will be placed on heap throughout its scope or till it is destroyed. It is not at all a performance issue because this class instantiates only four bytes and four doubles in it. So, they can be held safely throughout your application run with out any performance issues. And also as it is a singleton object, there will only be one static instance and thereby no multiple pointers, which is another good thing in its rigidity.

Using the class

Now lets go to the actual usage of the class. Download the zipped file which contains Header (.h), Implementation (.cpp) and Documentation files. Please refer and abide by its copyright.

Actually I wrote it for my MFC based project. But it is just a normal C++ class. It can be used on any platform and with any compiler if you have support for OpenGL. Don't forget to include OpenGL headers and Libraries in your project if you are using this class in your project.

Include the file "colour.h" as an interface, where ever you want to use this class. My suggestion is, to have a separate file (.h) in your project which specifies all the common and general purpose include files for your whole project. For example MFC based project will have a file called "stdafx.h" which has all common purpose Macros and Includes. This gives you the flexibility that you don't have to include this file everytime you want to use this class in your own class. You just need to include your ".h" file where you have all your includes and macros. It is just like having a common include file with all necessary includes.

After you have included it and had its interface ready, you can use it as you like and it is explained below.

Initialization

You have to initialize the singleton object once in your application. You can initialize it like this.

Colour::Initialize()

Initialize() is a static member function of the class. It instantiates the object as a static variable on the Heap. Don't try to Initialize multiple times. Evethough there is a guard against multiple initializations and nothing serious will happen, I recommend you only to initialize it once. You can initialize it where your application initializes itself. For example, in your OnCreate() function call, by OpenGL.

De-Initialization

You should not forget to Destroy the object you have just created with the funtion Initialize(). Otherwise you will end up with a memory leak in your application. But remember you have to destroy it only once. Multiple destroying attempts will give you Corrupted Heap exceptions.

The function you have to call is,

Colour::Destroy();

You can call it at a point where your application exits and performs clean up operations.

Specifying Colours

Now comes the actual use of this class. There are 109 colours coded in the class. In addition it provides you the facility to specify your own RGB values along with transparency if you haven't found your colour in this class.

So, if you want to specify OpenGL the colour Red, you can do it like this,

glColor4dv(Colour::Instance()->Red());

If you don't want to give importance to transparency, just call glColor3dv(...) instead of glColor4dv(...) like this,

glColor3dv(Colour::Instance()->Red());

Colour::Instance() will return you the static instance of the object that you initialized with the function Initialize(). From that object you are accessing the member function of the class 'Red()' which computes the OpenGL equivalent colour values of RGB components of 'Red' colour and returns you an array of four double values which specify RGBA values in the range 0 to 1 as OpenGL needs.

When this function call is done with glColor3dv(...) it only takes first three double values of the array that the function 'Red()' returns and when called with glColor4dv(...) all the four RGBA values will be used.

But, if you want to use transparency, you have to set the transparency of the class before you use the class's function calls with glColor4dv(...).

You can do it like this,

Colour::Instance()->Alpha(0.5)

The function Alpha(float A) sets the Alpha value of the class and subsequent calls to any of the other colour functions will take this alpha value into consideration. So, it is just like OpenGL pipeline architecture. Note that you have to specify the Alpha value in the range 0 to 1.

Specifying your own colours

Even though there are 109 colours in this class you will still have the need to specify your own colours using RGBA values.

To do this, you can call the GetColour(GLubyte R, GLubyte G, GLubyte B, GLubyte A=255) function like this,

glColor4dv(Colour::Instance()->GetColour(R, G, B, A));

R, G, B and A values are GLubytes. So they should be in the range 0 to 255. The fourth parameter in this fucntion's signature, A, is optional. By default its value will be set to 255, which means 'no transparency'.

So, you can also use this function like this,

glColor3dv(Colour::Instance()->GetColour(R, G, B));

if you don't want the transparency. Just the fourth double value in the array that GetColour(...) returns will be discarded by glColor3dv(...).

Accessing previously specified colour

For any reason, if you want to access previously specified colour without explicitly specifying it again, might be for the sake of test!, you can do it like this,

glColor3dv(Colour::Instance()->CurrColour()); or glColor4dv(Colour::Instance()->CurrColour());

This function returns you the previous colour that you specifed either with GetColour(...) function or with a call to one of the colour functions of this class, like 'Khaki()'. I did not use this function till now, but it will be useful for testing in some cases

Conclusion

So, this explains everything about the class. You can go to the Doxygen generated documentation page of this class. Alternatively, here is the list of all colour functions that this class has in it.

White
Black
Red
Green
Blue
Yellow
Cyan
Magenta
Violet
Indigo
Orange
Gray
Brown
Gold	
Snow
Ghostwhite
WhiteSmoke
Gainsboro
FloralWhite  
OldLace
Linen
AntiqueWhite
PapayaWhip
BlanchedAlmond
Bisque
PeachPuff
NavajoWhite
Moccasin
CornSilk
Ivory
LemonChiffon
SeaShell
HoneyDew
MintCream
Azure
AliceBlue
Lavender
LavenderBlush
MistyRose
DarkSlateGray
DimGray
SlateGray
LightSlateGray
LightGrey
MidnightBlue
NavyBlue
CornFlowerBlue
DarkSlateBlue
SlateBlue
MediumSlateBlue
RoyalBlue
DodgerBlue
DeepSkyBlue
SkyBlue
LightSkyBlue
SteelBlue
LightBlue
PowderBlue
PaleTurquoise
DarkTurquoise
Turquoise
CadetBlue
MediumAquamarine
DarkOliveGreen
DarkSeaGreen
SeaGreen
PaleGreen
SpringGreen
LawnGreen
ChartReUse
GreenYellow
ForestGreen
DarkKhaki
Khaki
RosyBrown
IndianRed
SaddleBrown
Sienna
Peru
BurlyWood
Beige
Wheat
SandyBrown
Tan
Chocolate
FireBrick
DarkSalmon
Salmon
DarkOrange
Coral
LightCoral
Tomato
OrangeRed
HotPink
DeepPink
Pink
PaleVioletRed
Maroon
MediumVioletRed
Plum
Orchid
BlueViolet
Purple
DarkGray
DarkCyan
DarkMagenta
DarkRed
LightGreen

You have to strictly abide by the copyright message that comes with this class. Go to the '.h' file after downloading the ZIP file from this page to refer to the Copyright message.

V Rama Aravind,
Oxford, 2005/09/23.

© 2003 - 2023, Rama Aravind Vorray, Inc. Site Last Updated: 2023-04-08. Contact Me